package com.slz3.business.module.configManagement.conZoneTemplate.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conZoneTemplate.domain.ConZoneTemplate;
import com.slz3.business.module.configManagement.conZoneTemplate.domain.ConZoneTemplateFactory;
import com.slz3.business.module.configManagement.conZoneTemplate.steward.ConZoneTemplateSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConZoneTemplateServiceImpl implements ConZoneTemplateService {
    @Override
    public ConZoneTemplate save(ConZoneTemplate conZoneTemplate) {
        return Repositorys.conZoneTemplate.save(ConZoneTemplateFactory.initBaseFields(conZoneTemplate));
    }
    @Override
    public Page<ConZoneTemplate> page(MultiValueMap<String, String> params) {
        Page<ConZoneTemplate> conZoneTemplatePage = Repositorys.conZoneTemplate.findAll(
                ConZoneTemplateSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conZoneTemplatePage;
    }


    @Override
    public ConZoneTemplate modify(ConZoneTemplate conZoneTemplateNew) {
        return Repositorys.conZoneTemplate.saveAndFlush(ConZoneTemplateFactory.initBaseFields(conZoneTemplateNew));
    }

    @Override
    public Optional<ConZoneTemplate> single(UUID uuid) {
        return Repositorys.conZoneTemplate.findById(uuid);
    }

    @Override
    public List<ConZoneTemplate> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conZoneTemplate.findAllById(uuids);
    }

    @Override
    public List<ConZoneTemplate> delete(List<ConZoneTemplate> conZoneTemplateList) {
        conZoneTemplateList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConZoneTemplateFactory.initBaseFields(item);
        });
        return Services.conZoneTemplateTransactional.batchModify(conZoneTemplateList);
    }

}
